package zdream.pmfield.stage.message;

import java.util.Objects;

import zdream.pmfield.stage.object.FoeActor;
import zdream.pmfield.stage.director.Roll;

/**
 * @author Zdream
 * @date 2023-11-14
 * @since 0.0.1
 */
public class RollReportMessage extends StageMessage {
	public final FoeActor foe;
	public final Roll[] rolls, moreRolls;
	public String title = "";

	public RollReportMessage(FoeActor foe, Roll[] rolls) {
		this(foe, rolls, new Roll[0]);
	}

	public RollReportMessage(FoeActor foe, Roll[] rolls, Roll[] moreRolls) {
		this.type = StageMessageType.REPORT;
		this.foe = Objects.requireNonNull(foe);
		this.rolls = Objects.requireNonNull(rolls);
		this.moreRolls = Objects.requireNonNull(moreRolls);

		for (Roll roll : rolls) {
			Objects.requireNonNull(roll);
		}
	}

	public RollReportMessage withTitle(String title) {
		this.title = title;
		return this;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(100);
		builder.append("[RR] ").append(foe.foe.getName()).append(' ').append("的");

		if (!Objects.equals(title, "")) {
			builder.append(' ').append(title).append(' ');
		}

		builder.append("判定结果:").append(' ');
		for (Roll roll : rolls) {
			builder.append('[');
			if (roll.focus) {
				builder.append("专注").append("->");
			} else if (roll.affectedBy != null) {
				builder.append(roll.affectedBy).append("->");
			}

			if (roll.result) {
				builder.append(roll.vStat.toString().charAt(0)).append(']');
				continue;
			}
			builder.append('_').append(']');
		}

		if (moreRolls.length > 0) {
			builder.append(' ').append('+').append(' ');
			for (Roll roll : moreRolls) {
				builder.append(roll.createdBy).append(':');
				builder.append('[');
				if (roll.affectedBy != null) {
					builder.append(roll.affectedBy).append("->");
				}

				if (roll.result) {
					builder.append(roll.vStat.toString().charAt(0)).append(']');
					continue;
				}
				builder.append('_').append(']');
			}
		}

		return builder.toString();
	}
}
